<?php
/**
 * Created by PhpStorm.
 * User: bolle
 * Date: 28.12.15
 * Time: 10:19
 */

namespace tbollmeier\alteraparser\syntaxgraph;


class RuleFactory
{
    private $instances;

    public function __construct()
    {
        $this->instances = [];
    }

    public function createRule($name, $ruleFn, $optParams=[])
    {
        $rule = new Rule($name, $ruleFn, $optParams);
        $this->instances[$name] = $rule;

        return $rule;
    }

    public function __call($name, $args)
    {
        if (array_key_exists($name, $this->instances)) {
            $rule = $this->instances[$name];
            return $rule->copy();
        } else {
            throw new \Exception("Rule $name does not exist.");
        }
    }

}